Bubble Sort Algorithm animationΒΆ

import turtle
from random import shuffle
from time import sleep

myPen = turtle.Turtle()
turtle.tracer(0)
myPen.speed(0)
myPen.color("#000000")
myPen.hideturtle()

topLeft_x = -180
topLeft_y = 160
intDim = 30
gap = 40

def text(message, x, y, size):
    FONT = ('Arial', size, 'normal')
    X = myPen.xcor()
    Y = myPen.ycor()
    myPen.penup()
    myPen.goto(x, y)
    myPen.color("#000000")
    myPen.write(message, align="left", font=FONT)
    myPen.goto(X, Y)
    myPen.pendown()

def display_code():
    global topLeft_x, topLeft_y
    offset_x = 30
    font_size = 14
    text("def bubble_sort(A):", topLeft_x - offset_x, topLeft_y + 420, font_size)
    text("    numberOfIterations = 0", topLeft_x - offset_x, topLeft_y + 390, font_size)
    text("    changed = True", topLeft_x - offset_x, topLeft_y + 360, font_size)
    text("    while changed:", topLeft_x - offset_x, topLeft_y + 330, font_size)
    text("        changed = False", topLeft_x - offset_x, topLeft_y + 300, font_size)
    text("        for i in range(0, len(A) - numberOfIterations - 1):", topLeft_x - offset_x, topLeft_y + 270, font_size)
    text("            if A[i] > A[i + 1]:", topLeft_x - offset_x, topLeft_y + 240, font_size)
    text("                # swap values", topLeft_x - offset_x, topLeft_y + 210, font_size)
    text("                A[i], A[i + 1] = A[i + 1], A[i]", topLeft_x - offset_x, topLeft_y + 180, font_size)
    text("                changed = True", topLeft_x - offset_x, topLeft_y + 150, font_size)
    text("        numberOfIterations += 1", topLeft_x - offset_x, topLeft_y + 120, font_size)

def display_code_01():
    global topLeft_x, topLeft_y
    offset_x = 20
    font_size = 14
    text("def bubble_sort(A):", topLeft_x - offset_x, topLeft_y + 300, font_size)
    text("    N = len(A)):", topLeft_x - offset_x, topLeft_y + 270, font_size)
    text("    for bypass in range(1, N):", topLeft_x - offset_x, topLeft_y + 240, font_size)
    text("        for k in range(0, N - bypass):", topLeft_x - offset_x, topLeft_y + 210, font_size)
    text("            if A[k] > A[k+1]:", topLeft_x - offset_x, topLeft_y + 180, font_size)
    text("                A[k], A[k+1] = A[k+1], A[k]", topLeft_x - offset_x, topLeft_y + 150, font_size)

# A procedure to draw the grid on screen using Python Turtle
def drawList(list, numberOfIterations):

    global topLeft_x, topLeft_y, intDim

    myPen.penup()
    myPen.goto(topLeft_x, topLeft_y)
    myPen.pendown()

    for i in range(0, len(list)):
        # myPen.goto(topLeft_x+i*intDim,topLeft_y-intDim)
        if i < len(list) - numberOfIterations:
            myPen.fillcolor("#FFFFFF")
        else:
            myPen.fillcolor("#FF00FF")

        myPen.begin_fill()
        for side in range(0, 4):
            myPen.forward(intDim)
            myPen.left(90)
        myPen.end_fill()

        myPen.forward(intDim)
        text(list[i], topLeft_x + i * intDim + 8, topLeft_y, 20)    # topLeft_y + 5

def highlightValues(list, position, color1, color2):

    global topLeft_x, topLeft_y, intDim, gap

    myPen.penup()
    myPen.goto(topLeft_x + position * intDim, topLeft_y + gap)
    myPen.pendown()
    myPen.fillcolor(color1)
    myPen.begin_fill()
    for step in range(0, 2):
        for side in range(0, 4):
            myPen.forward(intDim)
            myPen.left(90)
        myPen.forward(intDim)
        myPen.end_fill()
        myPen.fillcolor(color2)
        myPen.begin_fill()
    myPen.end_fill()

    text(list[position],
              topLeft_x + position * intDim + 8,
              topLeft_y + gap, 20)      # topLeft_y + 5
    text(list[position + 1],
              topLeft_x + (position + 1) * intDim + 8,
              topLeft_y + gap, 20)      # topLeft_y + 5
    myPen.getscreen().update()
    if color1 != "#FFFFFF":
        sleep(0.2)


# A function to sort a list using a Bubble Sort Algorithm
def bubbleSort(list):

    global topLeft_y, intDim, gap

    drawList(list, -1)
    topLeft_y = topLeft_y - gap

    drawList(list, -1)
    topLeft_y = topLeft_y - gap

    myPen.getscreen().update()
    sleep(1)
    numberOfIterations = 0
    changed = True

    display_code()                  # after topLeft_y in drawList
    # display_code_01()

    while changed:
        changed = False
        for i in range(0, len(list) - numberOfIterations - 1):
            highlightValues(list, i, "#CCCCCC", "#CCCCCC")
            if list[i] > list[i + 1]:
                highlightValues(list, i, "#FF66FF", "#FF66FF")
                # swap values
                list[i], list[i + 1] = list[i + 1], list[i]
                highlightValues(list, i, "#FF66FF", "#FF66FF")
                changed = True
            if i >= len(list) - numberOfIterations:
                highlightValues(list, i, "#FFFFFF", "#FFFFFF")
            else:
                highlightValues(list, i, "#FFFFFF", "#FF00FF")

        numberOfIterations += 1
        drawList(list, numberOfIterations)
        topLeft_y = topLeft_y - gap
        myPen.getscreen().update()
        sleep(0.5)
    text("Bubble Sort Complete", topLeft_x, topLeft_y, 16)
    text("Number of Iterations: " + str(numberOfIterations),
          topLeft_x, topLeft_y - 30, 16)
    myPen.getscreen().update()

def main():
    list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
    shuffle(list)
    my_win = turtle.Screen()
    bubbleSort(list)
    my_win.exitonclick()

if __name__ == "__main__":
    main()
    mainloop()